package com.neuedu.shop.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.shop.pojo.Category;
import com.neuedu.shop.service.CategoryService;

/**
 * @author ZhangHongRui
 *
 *         2019年12月28日
 */
@Controller
public class CategoryController {
	@Autowired
	private CategoryService service;

	@RequestMapping("/back/findAll.category")
	public String findAll(ModelMap map) {
		List<Category> categories = service.findAll();
		map.addAttribute("categories", categories);
		return "forward:category_list.jsp";
	}

	@RequestMapping("/back/addRoot.category")
	public String addRootCategory(String name, String descr) {
		service.addRootCategory(name, descr);
		return "redirect:findAll.category";
	}

	/*
	 * @RequestMapping("/back/findToTree.category") public String
	 * findToTree(ModelMap map) { List<Category> categories = service.findToTree();
	 * map.addAttribute("categories", categories); System.out.println(categories);
	 * 
	 * return "forward:category_list.jsp"; }
	 */

	@RequestMapping("/back/findToTree.category")
	@ResponseBody
	public List<Category> findToTree() {
		return service.findToTree();// [Category的容器]
	}

	@RequestMapping("/back/addChild.category")
	public String addChildCategory(String name, String descr, int pid) {
		service.addChildCategory(name, descr, pid);
		return "redirect:category_list.jsp";
	}

	@RequestMapping("/back/findById.category")
	public String findById(int id, ModelMap map) {
		Category c = service.findById(id);
		map.addAttribute("c", c);
		return "forward:category_addChild.jsp";
	}

	@RequestMapping("/back/findForUpdate.category")
	public String findForUpdate(int id, ModelMap map) {
		Category alter = service.findById(id);
		map.addAttribute("alter", alter);
		return "forward:category_alterDescr.jsp";
	}

	@RequestMapping("/back/alterDescr.category")
	public String alterDescr(Category category) {
		System.out.println(category);
		service.alterDescr(category);
		return "redirect:category_list.jsp";
	}

	@ResponseBody
	@RequestMapping("/back/findThree.category")
	public List<Category> findThree() {
		return service.findThree();
	}

}
